#pragma GCC optimize(2)
#include <bits/stdc++.h>

using namespace std;
#define int long long
const int inf = 0x3f3f3f3f3f3f3f3f;

void solve()
{
	int n, m;
	cin >> n >> m;
	int now = 0, t = 0;
	int ans = 0;
	while (n--)
	{
		int a, b;
		cin >> a >> b;
		if (now != b)
		{
			if ((a - t) & 1) ans += (a - t);
			else ans += (a - t) - 1;
		}
		else
		{
			if ((a - t) & 1) ans += (a - t) - 1;
			else ans += (a - t);
		}
		t = a;
		now = b;
	}
	if (t != m) ans += m - t;
	cout << ans << '\n';
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int _ = 1;
	cin >> _;
	while (_--)
	{
		solve();
	}
	return 0;
}